<?php

namespace app\admin\controller;

use app\common\controller\Send;
use app\common\enums\StatusEnum;
use think\facade\Request;
use app\admin\model\Slides AS SlidesModel;
use app\admin\validate\Slides AS SlidesValidate;
use app\admin\model\Lang;

class Slides extends Base
{
    use Send;

    /**
     * 列表
     * @return \think\response\View
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $search = input('');
        $langId = isset($search['lang_id']) ? $search['lang_id'] : -1;
        $status = isset($search['status']) ? $search['status'] : -1;
        $search = [
            'lang_id' => $langId,
            'status' => $status
        ];
        $where = [];
        if ($search['lang_id'] != null && $search['lang_id'] != -1) {
            $where[] = ['lang_id', '=', $search['lang_id']];
        }

        if ($search['status'] != null && $search['status'] != -1) {
            $where[] = ['status', '=', $search['status']];
        }

        $model = SlidesModel::getList($where);
        return view('index', [
            'model' => $model['model'],
            'count' => $model['count'],
            'search' => $search,
            'status' => StatusEnum::$listExplain,
            'lang' => Lang::getColumn()
        ]);
    }

    /**
     * 添加修改
     * @return \think\response\Json|\think\response\View
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function form()
    {
        if (Request::instance()->isPost()) {
            $data = input('post.');

            // 检测变量
            $validate = new SlidesValidate();
            if (!$validate->check($data)) {
                return json(self::sendResult(102, [], $validate->getError()));
            }

            $langId = SlidesModel::saveOne($data);
            $code = 602;
            if ($langId > 0) {
                $code = self::$okCode;
            }
            return json(self::sendResult($code, [
                'url' => url('index'),
            ]));
        } else {
            $id = input('id') ? input('id') : 0;
            $model = [];
            $title = '添加首页轮播图';
            if ($id > 0) {
                $title = '编辑首页轮播图';
                $model = SlidesModel::get($id);
            }
            return view('form', [
                'status' => StatusEnum::$listExplain,
                'model' => $model,
                'lang' => Lang::getColumn(),
                'title' => $title
            ]);
        }
    }

    /**
     * 删除
     * @param $id
     * @return array
     */
    public function delete($id)
    {
        SlidesModel::deleteOne($id);
        return self::sendResult(self::$okCode, [
            'url' => url('index'),
        ]);
    }

    /**
     * 修改状态
     * @param $id
     * @return array
     */
    public function disabled($id)
    {
        SlidesModel::disabledOne($id);
        return self::sendResult(self::$okCode, [
            'url' => url('index'),
        ]);
    }

    public function deletes()
    {
        $ids = input('ids');
        // 删除数据
        SlidesModel::deleteAll($ids);
        return self::sendResult(self::$okCode, [
            'url' => url('index'),
        ]);
    }
}
